JLCA Companion Edition

ZipFileExtended.java

 
/**
 * ZipFileExtended.java
 *
 * @author  Artinsoft
 */
import java.io.*;
import java.util.zip.*;
import java.util.*;


public class ZipFileExtended extends java.util.zip.ZipFile{


    /** Extends the zip file class in order to provide aditional funtinality
     * for  decompressing  directories.
* * @param zipName the name of the zip file */ public ZipFileExtended(String zipName) throws java.io.IOException { super(zipName); } public void unZip(){ // //Unzips the contents of the file try { for (Enumeration entries = this.entries(); entries.hasMoreElements();) { // Get the entry name ZipEntry e = ((ZipEntry)entries.nextElement()); String fileName = e.getName(); if(e.isDirectory()) { System.out.println("Creating directory: " + e.getName()); (new File(e.getName())).mkdir(); continue; } if (fileName != null){ int lastIndex = java.lang.Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\')); //if the file is in a directory, determine if the directory exists if (lastIndex>-1){ File d = new File(fileName.substring(0,lastIndex)); if (!d.exists()){ System.out.println("Creating directory: " + fileName.substring(0,lastIndex)); d.mkdirs(); } } System.out.println("Extracting: " + fileName); copyInputStream(this.getInputStream(e), new BufferedOutputStream(new FileOutputStream(e.getName()))); } } } catch (Exception ioe) { System.err.println("Unhandled exception:"); ioe.printStackTrace(); return; } } /** Overrides the toString method * @return A printable list of the entries contained in the zip file. */ public String toString() { String res = ""; // Enumerate each entry for (Enumeration entries = this.entries(); entries.hasMoreElements();) { // Get the entry name res = res + ((ZipEntry)entries.nextElement()).getName() +"\n"; } return res; } private static final void copyInputStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); out.close(); } }

©2004 ArtinSoft. All rights reserved